package com.tripcube.ai.config;

import com.aliyun.bailian20231229.Client;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里百炼知识库配置类
 * 
 * @author alander
 */
@Configuration
@Slf4j
public class BailianKnowledgeBaseConfig {
    
    @Value("${alibaba.cloud.access-key-id}")
    private String accessKeyId;
    
    @Value("${alibaba.cloud.access-key-secret}")
    private String accessKeySecret;

    @Value("${alibaba.cloud.endpoint:bailian.cn-beijing.aliyuncs.com}")
    private String endpoint;

    @Value("${alibaba.cloud.workspace-id}")
    private String workspaceId;
    
    /**
     * 创建阿里百炼知识库客户端
     * 
     * @return 百炼知识库客户端实例
     * @throws Exception 当配置信息无效或网络连接失败时抛出异常
     */
    @Bean
    public Client bailianClient() throws Exception {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret)
                .setEndpoint(endpoint);
        
        log.info("初始化阿里百炼知识库客户端，endpoint: {}", endpoint);
        return new Client(config);
    }
    
    /**
     * 获取业务空间ID
     * 
     * @return 业务空间ID字符串
     */
    @Bean
    public String bailianWorkspaceId() {
        return workspaceId;
    }
}